/*
 * File:   serialUltrasonic.h
 * Author: Sam Crow
 *
 * Created on March 4, 2012, 4:58 PM
 */

#ifndef SERIALULTRASONIC_H
#define	SERIALULTRASONIC_H

#include <iostream>
#include <string>

#include <stdio.h>    /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h>   /* Standard types */
#include <string.h>   /* String function definitions */
#include <unistd.h>   /* UNIX standard function definitions */
#include <fcntl.h>    /* File control definitions */
#include <errno.h>    /* Error number definitions */
#include <termios.h>  /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#include <getopt.h>

/**
 * This class interfaces with the ultrasonic sensor over a serial interface
 */
class SerialUltrasonic {
public:
	/**
	 * Constructor
     * @param port The path to the resource on the file system (On linux/unix,
	 *  usually in /dev) representing the serial port
     * @param baud The baud rate to connect at
     */
    SerialUltrasonic(std::string port, int baudRate);

	/**
	 * Get an integer representation of the number of inches to the nearest object
	 * detected by the sensor
     * @return The distance in inches
     */
    int getDistance();
	/**
	 * Get a string representation of the number of inches to the nearest object
	 * detected by the sensor
     * @return The distance in inches
     */
	std::string getDistanceString();

	/**
	 * Destructor
     */
    virtual ~SerialUltrasonic();
private:

	/** The system-provided reference to the serial port */
    int portID;

	/**
	 * Open a serial port
     * @param name The path to the resource on the file system (On linux/unix,
	 *  usually in /dev) representing the serial port
     * @param baud The baud rate to connect at
     * @return The system-provided reference to the serial port
     */
    int openPort(std::string name, int baud);

	/**
	 * Read data from the serial port until a given character is received
     * @param fd The system-provided reference to the serial port
     * @param buf The char[] array that data read will be written into
     * @param until The character on which to stop reading and return
     * @return The number of bytes read
     */
    int readUntil(int fd, char* buf, char until);

	/**
	 * Check if every character in a given string is a numerical digit 0-9
     * @param input The input string to check
     * @return If the string passes the test
     */
	bool isNumeric(std::string input);
};

#endif	/* SERIALULTRASONIC_H */

